<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

class PageRenderer
{

	var $title_delim = ' / ';
	var $template_dir;
	var $template = 'page.html';
	

	var $meta_title;
	var $meta_keyword;
	var $meta_charset;
	
	var $vars = array('meta_title' 		=> 'Title',  
					  'meta_keyword' 	=> '',  
					  'meta_description'=> '',  
					  'meta_charset' 	=> ''
					   );
	
	
	
    function PageRenderer($engine = 'tpl_templatez') {
        
		$this->view = &$view;
		$this->template_dir = APP_TMPL_DIR;
		
		$this->tpl = &$this->setEngine($engine);
    }
	
	
	function &setEngine($engine) {
		if($engine == 'tpl_templatez') {
			$t = &new PageRenderer_templatez();
			return $t;
		
		} elseif($engine == 'smarty') {
			$t = &new PageRenderer_smarty();
			return $t;
		} 
	}
	
	
	function render() {
		return $this->tpl->render($this->vars, $this->template_dir, $this->template);	
	}	
	
	

/*
	function assign1($var, $value = false) {
		
		if(is_array($var)) {
			foreach($var as $k => $v) {
			//foreach(array_keys($var) as $k) {
				$this->vars[$k] = &$var[$k];
			}
		} else {
			if($value === false) {
				trigger_error($this->error_pref. " Can't assign value for var - <b>".$var."</b>. Missing argument 2 for tplAssign();");
			} else { 
				$this->vars[$var] = &$value; 
			}
		}
	}
*/

	
	
	function assign(&$var, $value = false) {
		
		if($value) {
			$this->vars[$value] = &$var; 
		
		} else {
		
			if(is_array($var)) {
				foreach($var as $k => $v) {
					$this->vars[$k] = &$var[$k];
				}
			} else {
				if($value === false) {
					trigger_error($this->error_pref. " Can't assign value for var - <b>".$var."</b>. Missing argument 2 for tplAssign();");
				} else { 
					$this->vars[$value] = &$var; 
				}
			}			
		}
	}
	
	
	
	function setTemplate($var) {
		$this->template = $var;
	}
	
	
	function display() {
		echo $this->render();
	}
}



class PageRenderer_templatez
{
	
	function render($vars, $template_dir, $template) {
		
		$tpl = &new tplTemplatez($template_dir . $template);
		$tpl->s_var_tag = '{';
		$tpl->tplParse($vars);
		return $tpl->tplPrint(1);		
	}	
}



class PageRenderer_smarty
{
	
	function render($vars, $template_dir, $template) {
		
		$tpl = &new CommonSmarty();
		$tpl->template_dir = $template_dir;
		$tpl->assign($vars);
		return $tpl->fetch($template);
	}	
}

?>